/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/<a href="https://www.kaaction.com">KAA</a>"><a href="https://www.kaaction.com">KAA</a></a> All rights reserved.
 */
package com.kaaction.modules.ka.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.DateUtils;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.common.utils.excel.ExportExcel;
import com.kaaction.modules.ka.entity.ProductManage;
import com.kaaction.modules.ka.entity.Statistics;
import com.kaaction.modules.ka.service.BasicGoodsService;
import com.kaaction.modules.ka.service.ProductManageService;

/**
 * 库存盘点Controller
 * @author huanghaiming
 * @version 2017-04-25
 */
@Controller
@RequestMapping(value = "${adminPath}/ka/checkInventory")
public class CheckInventoryController extends BaseController {

	@Autowired
	private ProductManageService productManageService;
	
	@Autowired
	private BasicGoodsService basicGoodsService;
	
	@ModelAttribute
	public ProductManage get(@RequestParam(required=false) String id) {
		
		ProductManage entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = productManageService.get(id);
			entity.setListPro(basicGoodsService.getProductCode(entity)) ;
		}
		if (entity == null){
			entity = new ProductManage();
		}
		return entity;
	}
	
	@RequiresPermissions("ka:checkInventory:view")
	@RequestMapping(value = {"list", ""})
	public String list(ProductManage productManage, HttpServletRequest request, HttpServletResponse response, Model model) {
		Statistics statistics =productManageService.selStatistics(productManage);
		model.addAttribute("statistics", statistics);
		productManage.getSqlMap().put("dsf", "and a.state in ('0','1','2','3','4','5','6','7')");
		Page<ProductManage> page = productManageService.findPage(new Page<ProductManage>(request, response), productManage); 
		model.addAttribute("page", page);
		return "modules/ka/checkInventoryList";
	}

	@RequiresPermissions("ka:checkInventory:view")
	@RequestMapping(value = "form")
	public String form(ProductManage productManage, Model model) {
		model.addAttribute("productManage", productManage);
		return "modules/ka/checkInventoryForm";
	}

	@RequiresPermissions("ka:checkInventory:edit")
	@RequestMapping(value = "save")
	public String save(ProductManage productManage, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, productManage)){
			return form(productManage, model);
		}
		productManageService.save(productManage);
		addMessage(redirectAttributes, "保存库存盘点成功");
		return "redirect:"+Global.getAdminPath()+"/ka/checkInventory/?repage";
	}
	
	@RequiresPermissions("ka:checkInventory:edit")
	@RequestMapping(value = "delete")
	public String delete(ProductManage productManage, RedirectAttributes redirectAttributes) {
		productManageService.delete(productManage);
		addMessage(redirectAttributes, "删除库存盘点成功");
		return "redirect:"+Global.getAdminPath()+"/ka/checkInventory/?repage";
	}
	
	@RequiresPermissions("ka:checkInventory:delete")
	@RequestMapping(value = "deleteAll")
	public String deleteAll(ProductManage productManage, RedirectAttributes redirectAttributes) {
		productManageService.deleteAll(productManage);
		addMessage(redirectAttributes, "删除库存盘点成功");
		return "redirect:"+Global.getAdminPath()+"/ka/checkInventory/?repage";
	}
	
	/**
	 * 导出库存盘点数据
	 * @param user
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("ka:checkInventory:view")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public String exportFile(ProductManage productManage, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "库存盘点数据"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            productManage.getSqlMap().put("dsf", "and a.state in ('0','1','2','3','4','5','6','7')");
            List<ProductManage> list = productManageService.findProductManage(productManage);
    		new ExportExcel("库存盘点数据", ProductManage.class).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出库存盘点数据失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/ka/checkInventory/?repage";
    }
	
	@RequiresPermissions("ka:productManage:view")
	@RequestMapping(value = "checkInventoryForm")
	public String checkInventoryForm(ProductManage productManage, Model model) {
		model.addAttribute("productManage", productManage);
		return "modules/ka/checkPutawayForm";
	}
	
	/**
	 * 质检上架
	 * @param productManage
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("ka:checkInventory:edit")
	@RequestMapping(value = "stayPutaway")
	public String stayPutaway(ProductManage productManage, Model model, RedirectAttributes redirectAttributes) {
		
		int cnt = productManageService.getCount(productManage);//是否超出质检数量
		
		if(cnt==0){
			model.addAttribute("message", "超出库存质检数量");
			return checkInventoryForm(productManage, model);
		}
		productManage.setState("1");
		productManageService.putawayProcess(productManage);
		addMessage(redirectAttributes, "库存质检成功");
		return "redirect:"+Global.getAdminPath()+"/ka/checkInventory/?repage";
	}
}